#include <reg52.h>
#include<intrins.h>

/********begin*****驱动函数定义*********begin********/

//uchar num[] = "0123456789";	
//#define SHOW_DATA P0
#define SHOW_DATA P2
#define DATA P2
//#define DATA P0
unsigned char num[] = "0123456789";
char *sentence = "codebykeil5";
sbit RS = P3^0;
sbit RW = P3^1;
sbit E  = P3^2;	

unsigned char i = 10;

void delay(char ms);
void delayms(char ms);
void C51CD1602_init(void);
void FLCD1602_init(void);
void FLCD1602_clear();
void display_string(unsigned char *p);
void display_num(unsigned char x);
void gotoxy(unsigned char y,unsigned char x);
void write_command(unsigned char cmd);
void write_data(unsigned char dat);
char is_busy();
void pos (unsigned char pos);

/**************begin2*********************/
   typedef unsigned char u8;
   typedef unsigned int u16;
	 
	 unsigned char number1;
	 unsigned char number2;
 
   #define GPIO_DIG P0
   #define GPIO_KEY P1
	 
	 u8 KeyColValue;
   u8 KeyLineValue;
	 
	 void delay_u16(u16 i); //延时函数
	 void KeyDown(); //键盘按键扫描函数
	 
/****************end2**********************/

/********end*****驱动函数定义*********end********/

void main()
{
	  C51CD1602_init();    // for LCD1602 initialization
	
	  write_command(0x80); // for position initialization

//    pos (0);           // this function counld be used
	
//	  FLCD1602_init();   // for LCD1602 initialization
	
    write_data('1');
    write_data('2');
    write_data('3');
    write_data('4');
    write_data('5');
    write_data('6');
    write_data('7');
    write_data('8');
	  /***
    write_data('9');
    write_data('0');
	  ***/
	  display_num(90);

//    write_command(0x80); //test for position
	
    for(i = 0;i<6;i++){    //write_data 和 write_commmand 都要添加延时函数才能准确显示
		write_data('*');	     //不然会有空白或不显示
		}


//    pos (0x40);          //this function counld be used
		
		write_command(0x80+0x40); // for position initialization
		
    write_data('C');
    write_data('h');
    write_data('i');
    write_data('n');
    write_data('a');
		
		display_string(sentence);
		
		write_command(0x01);//光标清0 指针清0

		pos(0);
    while(1){
			KeyDown();//检测按键是否按下
		  display_num(KeyLineValue*4 + KeyColValue);
			delayms(50);
			pos(0);
			/***
			number1 = (KeyLineValue*4 + KeyColValue)/10;
			number2 = (KeyLineValue*4 + KeyColValue)%10;
			write_data(number1);
      write_data(number2);
			delayms(1000);	
      ***/			
		}
}


/********begin*****驱动函数定义*********begin********/

void C51CD1602_init(void){
	  E = 0;
    //初始化 复位 lcd1602
    write_command(0x38);//设置显示模式 指令码 00111000 => 0x38
    delayms(1);
    write_command(0x0c);//开显示 不显示光标 不闪烁
    delayms(1);
    write_command(0x06);//光标设置 写字符后指针加一
    delay(1);
    write_command(0x01);//光标清0 指针清0
    delayms(1);	
}

void delay(char x)
{
	while(x--);	
} 
/***向LCD写一个命令**/
void write_command(unsigned char command)
{
	while(is_busy());
	RW = 0;
	RS = 0;
	E = 0;
	//P0 = command;
	//P2 = command;
	SHOW_DATA = command;
	_nop_();
	E = 1;
	//delay(100);//等待接收，忽略读状态操作
	E = 0;
	//RW = 1;
	delayms(1);
} 
/***向LCD写一个数据**/
void write_data(unsigned char date)
{
	while(is_busy());
	RW = 0;
	RS = 1;
	E = 0;
	//P0 = date;
	//P2 = date;
	SHOW_DATA = date;
	_nop_();
	E = 1;
	//delay(100);//等待接收
	E = 0;
	//RW = 1;
	delayms(1);
} 
/**初始化**/
void FLCD1602_init(void)
{
	E = 0;
	write_command(0x38);//两行，每行16个字符，每个字符是5*7点阵
	write_command(0x0f);//光标显示并闪烁
	//write_command(0x0c);//光标不显示
	write_command(0x06);//光标随字符右移
}  
/**清屏**/
void FLCD1602_clear()
{
	E = 0;
	write_command(0x01);
	write_command(0x02);
}
/**向LCD写字符串**/
void display_string(unsigned char *p)
{
	while(*p)//字符非空
	{
		write_data(*p);//写字符
		p++;             //数据指针++
	}
}
/**向LCD写数字变量**/
void display_num(unsigned char x)
{
	write_data(num[x / 10]);
	write_data(num[x % 10]);
}
/**定位**/
void gotoxy(unsigned char y,unsigned char x)
{
	if(y == 1)
	write_command(0x40 + x);
	else if(y == 2)
	write_command(0x80 + 0x40 + x);
}				

void delayms(char ms)        //@12.000MHz
{
    while(ms--)
    {
        unsigned char i, j;
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}

char is_busy()
{
    int a=0;
    char result;
    RS = 0;//发送的是命令
    RW = 1;//读
    //_nop_();
    E  = 1;//使能
    _nop_();
    result = (1<<7 & DATA);//7bit 1 忙 0 不忙
    E = 0;//取消使能
    return result;
}

void pos(unsigned char pos)
{
    //设置指针位置 0x80 | 位置
    //write_command(pos|0x80);
	  write_command(pos+0x80);
}

/********end*****驱动函数定义*********end********/


/********begin*****按键扫描函数定义*********begin********/

void delay_u16(u16 i) //延时函数
  {
   while(i --);
  }
 
  void KeyDown() //键盘按键扫描函数
  {
 
  char a;
 
  GPIO_KEY = 0x0f;
  if(GPIO_KEY != 0x0f)//检测4行中哪一行按键是否按下
  {
   //delay_u16(1000); //延时消抖
		delayms(10);
   if(GPIO_KEY != 0x0f) //再次检测4行中哪一行按键是否按下
   {
   switch(GPIO_KEY) //根据IO的值来确定哪一行按键按下
   {
   case(0x07): KeyColValue = 0; break;
   case(0x0b): KeyColValue = 1; break;
   case(0x0d): KeyColValue = 2; break;
   case(0x0e): KeyColValue = 3; break;
   }
   }
  }
 
  GPIO_KEY = 0xf0;
  if(GPIO_KEY != 0xf0) //检测4行中哪一列按键是否按下
  {
   //delay_u16(1000);  //延时消抖
		delayms(10);
   if(GPIO_KEY != 0xf0) //再次检测4行中哪一列按键是否按下
   {
   switch(GPIO_KEY) //根据IO的值来确定哪一列按键按下
   {
   case(0x70): KeyLineValue = 0; break;
   case(0xb0): KeyLineValue = 1; break;
   case(0xd0): KeyLineValue = 2; break;
   case(0xe0): KeyLineValue = 3; break;
   }
   }
 
   while((a < 50) && (GPIO_KEY != 0xf0))  //延时，确保没有按键再按下
  {
   //delay_u16(1000);
		delayms(10);
    a ++;
  }
 
  }
}	

/********end*****按键扫描函数定义*********end********/
